package org.elepower.controller;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.elepower.dao.PoleDao;
import org.elepower.pojo.PsPoleInfo;
import org.elepower.pojo.PsUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/psPole")
public class PsPoleController {

    @Autowired
    private PoleDao poleDao;

    @RequestMapping("/index")
    public String index(ModelMap modelMap) throws Exception {
        modelMap.put("poleList", poleDao.getAll());
        return "system/pole/index";
    }

    @RequestMapping("/list")
    @ResponseBody
    public Map<String, Object> list(@RequestParam(defaultValue = "1") Integer page, @RequestParam(defaultValue = "10") Integer limit, PsPoleInfo condition) throws Exception {

        Map<String, Object> map = new HashMap<>();

        Page<PsPoleInfo> pager = PageHelper.startPage(page, limit);
        List<PsPoleInfo> list = poleDao.getlist(condition);

        map.put("code", 0);
        map.put("msg", "");
        map.put("data", list);
        map.put("count", pager.getTotal());
        return map;
    }

    @RequestMapping("/delete")
    @ResponseBody
    public Map<String, Object> delete(Integer[] ids) throws Exception {
        Map<String, Object> map = new HashMap<String, Object>();
        poleDao.delete(ids);
        map.put("result", true);
        return map;
    }

    @RequestMapping("/form")
    public String form(ModelMap modelMap) throws Exception {
        modelMap.put("poleList", poleDao.getAll());
        return "system/pole/form";
    }

    @RequestMapping(value = "/add", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> add(PsPoleInfo pole, HttpSession session) throws Exception {

        PsUser user = (PsUser)session.getAttribute(Constants.LOGIN_USER);
        Map<String, Object> map = new HashMap<>();

        if(pole.getIsStart() == null){
            pole.setIsStart(0);
        }
        //创建人
        pole.setCreatedBy(user.getUsername());

        poleDao.add(pole);
        map.put("result",true);
        return map;
    }

    @RequestMapping("/update")
    @ResponseBody
    public Map<String,Object> update(PsPoleInfo pole,HttpSession session) throws Exception{

        PsUser user = (PsUser)session.getAttribute(Constants.LOGIN_USER);
        Map<String,Object> map = new HashMap<>();

        if(pole.getIsStart() == null){
            pole.setIsStart(0);
        }

        pole.setLastUpdateTime(new Date());
        //创建人
        pole.setCreatedBy(user.getUsername());

        poleDao.update(pole);
        map.put("result",true);
        return map;
    }

    @RequestMapping("/getPole")
    @ResponseBody
    public List<PsPoleInfo> getPole() throws Exception{
        return poleDao.getPole();
    }

    @RequestMapping(value = "/view")
    @ResponseBody
    public PsPoleInfo view(Integer id) throws Exception {
        return poleDao.get(id);
    }



}
